<?php

require_once("header.php");

if ( $_SESSION['logon'] !== true ) {

	echo 'Login first';

} else {

	$postid = $_POST['postid'];
	$checked = $_POST['checked'];
	
	if ( !preg_match("/^[0-9]{1,10}$/i", $postid) ) {
		echo 'Please choose post';
		exit(0);
	}
	if ( !preg_match("/^[01]$/", $checked) ) {
		echo 'Please choose favourite or not';
		exit(0);
	}
	
	if ( $checked == 0 ) {
		$delete = $db->query("Delete From favourite Where post_id = $postid And user_id = ".$_SESSION['user_id']);
	} else {
		$new = $db->query("Insert Into favourite(post_id, user_id, favourite_time) values($postid, ".$_SESSION['user_id'].", '".date('Y-m-d H:i:s')."')");
	}

	echo "OK";

}

?>